package dialog.program;

import java.util.*;
import java.io. *;

import javax.servlet.*;
import javax.servlet.http.*;

@SuppressWarnings("serial")
public final class DialogServlet extends HttpServlet 
{
	static Library library = new Library(); 
	static Printer printer = new Printer();
	@SuppressWarnings("static-access")
	public static void printTabs(String cmd, HttpServletResponse response)
	throws IOException, ServletException
	{
		if (cmd != null) 
		{
			if (cmd.startsWith("add_user"))
			{
				printer.print_add_user_form(response);			
			}
			else if (cmd.startsWith("add_book"))
			{
				printer.print_add_book_form(response);
			}
			else if (cmd.startsWith("add_report"))
			{
				printer.print_add_report_form(response);
			}
			else if (cmd.startsWith("add_journal"))
			{
				printer.print_add_journal_form(response);
			}
			else if (cmd.startsWith("add_int_user"))
			{
				printer.print_add_int_user_form(response);
			}
			else if (cmd.startsWith("print_user"))
			{
				printer.print_user_form(response);
			}
			else if (cmd.startsWith("print_doc"))
			{
				printer.print_book_form(response);
			}
			else if (cmd.startsWith("print_all_users"))
			{
				printAllUsers(response);
			}
			else if (cmd.startsWith("print_all_documents"))
			{
				printAllDocs(response);
			}
		}
	}
	public void doGet(HttpServletRequest request, HttpServletResponse response) 
    	throws IOException, ServletException 
    {		
		PrintWriter writer = response.getWriter();
		try
		{
			String global_command = request.getParameter("global_tab");
			String command = request.getParameter("tab");
			printGlobalTabs(global_command, response);
			printTabs(command, response);
			command = processTab(command, request);
			if (command != null) 
			{
				processCommand(command, response);	
			}
	    }
	    catch (IOException e)
	    {
	    	System.err.println("IO error in program.");
	    	writer.println("<H2>Error during the program</H2>");
	    }
	    finally
	    {
	    	response.setContentType("text/html");
	    	writer.println("</body></html>");
	    }
    }
	public static void printGlobalTabs(String cmd, HttpServletResponse response)
		throws IOException, ServletException 
	{
		PrintWriter writer = response.getWriter();
		writer.println("<head><meta http-equiv=content-type content=text/html; charset=UTF-8><title>Library System</title></head>");
		writer.println("<center><H3>Library System</H3></center>" + 
				"<form name=Form2 action=http://www.csf108library.appspot.com/dialog>" +
				"<p style=\"text-align: center;\">" +
				"<button style=\"width: 150px;\" name=global_tab value=\"adding\">Adding</button>" +
				"<button style=\"width: 150px;\" name=global_tab value=\"processing\">Processing</button>" +
				"<button style=\"width: 150px;\" name=global_tab value=\"printing\">Printing</button>" +
				"<button style=\"width: 150px;\" name=global_tab value=\"removing\">Removing</button>"+
				"</p></form>");
		if (cmd != null) 
		{
			if (cmd.startsWith("adding"))
			{
				writer.println("<center>" +
						"<form name=Form1 action=http://www.csf108library.appspot.com/dialog>" +
						"<p style=\"text-align: center;\">" +
						"<button style=\"width: 150px;\" name=tab value=\"add_user\">Add User</button></p>" +
						"<button style=\"width: 150px;\" name=tab value=\"add_int_user\">Add User With ID</button></p>" +
						"<button style=\"width: 150px;\" name=tab value=\"add_book\">Add Book</button></p>" +
						"<button style=\"width: 150px;\" name=tab value=\"add_report\">Add Report</button></p>" +
						"<button style=\"width: 150px;\" name=tab value=\"add_journal\">Add Journal</button></p>" +
						"</center></form>");
			}
			else if (cmd.startsWith("removing"))
			{
				writer.println("<center>" +
						"<form name=Form1 action=http://www.csf108library.appspot.com/dialog>" +
						"<p style=\"text-align: center;\">" +
						"<button style=\"width: 150px;\" name=tab value=\"remove_user\">Remove User</button></p>" +
						"<button style=\"width: 150px;\" name=tab value=\"remove_document\">Remove Document</button></p>" +
						"</center></form>");
			}
			else if (cmd.startsWith("printing"))
			{
				writer.println("<center>" +
						"<form name=Form1 action=http://www.csf108library.appspot.com/dialog>" +
						"<p style=\"text-align: center;\">" +
						"<button style=\"width: 150px;\" name=tab value=\"print_user\">Print User</button></p>" +
						"<button style=\"width: 150px;\" name=tab value=\"print_doc\">Print Document</button></p>" +
						"<button style=\"width: 150px;\" name=tab value=\"print_loans\">Print Loans</button></p>" +
						"<button style=\"width: 150px;\" name=tab value=\"print_all_users\">Print All Users</button></p>" +
						"<button style=\"width: 150px;\" name=tab value=\"print_all_documents\">Print All Documents</button></p>" +
						"</center></form>");				
			}
			else if (cmd.startsWith("processing"))
			{
				writer.println("<center>" +
						"<form name=Form1 action=http://www.csf108library.appspot.com/dialog>" +
						"<p style=\"text-align: center;\">" +
						"<button style=\"width: 150px;\" name=tab value=\"borrow_doc\">Borrow Document</button></p>" +
						"</center></form>");				
			}
		}
	}
	public static String processTab(String cmd, HttpServletRequest request)
		throws IOException, ServletException
	{
		if (request.getParameter("name") != null && request.getParameter("address") != null && request.getParameter("phone") != null && request.getParameter("id") != null) 
		{
			cmd = "addIntUser ";
			if (request.getParameter("name") != "") 
			{
				cmd += request.getParameter("name");
				if (request.getParameter("address") != "") cmd += "/ " + request.getParameter("address");
				if (request.getParameter("phone") != "") cmd += "/ " + request.getParameter("phone");
				if (request.getParameter("id") != "") cmd += "/ " + request.getParameter("id");
			}
		}
		else if (request.getParameter("name") != null && request.getParameter("address") != null && request.getParameter("phone") != null) 
		{
			cmd = "addUser ";
			if (request.getParameter("name") != "") 
			{
				cmd += request.getParameter("name");
				if (request.getParameter("address") != "") cmd += "/ " + request.getParameter("address");
				if (request.getParameter("phone") != "") cmd += "/ " + request.getParameter("phone");
			}			
		}
		else if (request.getParameter("title_book") != null && request.getParameter("author_book") != null && request.getParameter("isbn") != null) 
		{
			cmd = "addBook ";
			if (request.getParameter("title_book") != "") 
			{
				cmd += request.getParameter("title_book");
				if (request.getParameter("author_book") != "") cmd += "/ " + request.getParameter("author_book");
				if (request.getParameter("isbn") != "") cmd += "/ " + request.getParameter("isbn");
			}
		}
		else if (request.getParameter("title_report") != null && request.getParameter("reference") != null && request.getParameter("author") != null) 
		{
			cmd = "addReport ";
			if (request.getParameter("title_report") != "")
			{
				cmd += request.getParameter("title_report");
				if (request.getParameter("reference") != "") cmd += "/ " + request.getParameter("reference");
				if (request.getParameter("author") != "") cmd += "/ " + request.getParameter("author");
			}
		}
		else if (request.getParameter("title_journal") != null) 
		{
			cmd = "addJournal ";
			if (request.getParameter("title_journal") != "") cmd += request.getParameter("title_journal");
		}
		else if (request.getParameter("user_id") != null) 
		{
			cmd = "printUser ";
			if (request.getParameter("user_id") != "") cmd += request.getParameter("user_id");
		}
		else if (request.getParameter("doc_id") != null) 
		{
			cmd = "printDoc ";
			if (request.getParameter("doc_id") != "") cmd += request.getParameter("doc_id");
		}
		return cmd;
	}
	public static void print_add_user_form(HttpServletResponse response)
		throws IOException, ServletException
	{
		PrintWriter writer = response.getWriter();
		writer.println("<center><b>Add User:</b>" + 
			"<form name=Form1 action=http://www.csf108library.appspot.com/dialog>" +
			"<table>" +
			"<tr>" +
				"<td><B>Name:</B></td>" +
				"<td><input type=textbox name=name value=\"\" size=25></td><br>" +
			"</tr>" +
			"<tr>" +
				"<td><B>Address:</B></td>" +
				"<td><input type=textbox name=address value=\"\" size=25></td>" +
			"</tr>" +
			"<tr>" +
				"<td><B>Phone:</B></td>" +
				"<td><input type=textbox name=phone value=\"\" size=25></td>" +
			"</tr>" +
			"</table>" +
			"<input type=submit>" +
		"</form></center>");
	}
	public static void print_add_book_form(HttpServletResponse response)
		throws IOException, ServletException
	{
		PrintWriter writer = response.getWriter();
		writer.println("<center><b>Add Book:</b>" + 
				"<form name=Form1 action=http://www.csf108library.appspot.com/dialog>" +
			"<table>" +
			"<tr>" +
				"<td><B>Title:</B></td>" +
				"<td><input type=textbox name=title_book value=\"\" size=25></td><br>" +
			"</tr>" +
			"<tr>" +
				"<td><B>Author:</B></td>" +
				"<td><input type=textbox name=author_book value=\"\" size=25></td>" +
			"</tr>" +
			"<tr>" +
				"<td><B>ISBN:</B></td>" +
				"<td><input type=textbox name=isbn value=\"\" size=25></td>" +
			"</tr>" +
			"</table>" +
			"<input type=submit>" +
		"</form></center>");
	}
	public static void print_add_report_form(HttpServletResponse response)
		throws IOException, ServletException
	{
		PrintWriter writer = response.getWriter();
		writer.println("<center><b>Add Report:</b>" + 
				"<form name=Form1 action=http://www.csf108library.appspot.com/dialog>" +
				"<table>" +
				"<tr>" +
					"<td><B>Title:</B></td>" +
					"<td><input type=textbox name=title_report value=\"\" size=25></td><br>" +
				"</tr>" +
				"<tr>" +
					"<td><B>Reference:</B></td>" +
					"<td><input type=textbox name=reference value=\"\" size=25></td>" +
				"</tr>" +
				"<tr>" +
					"<td><B>Author:</B></td>" +
					"<td><input type=textbox name=author value=\"\" size=25></td>" +
				"</tr>" +
				"</table>" +
				"<input type=submit>" +
		"</form></center>");
	}
	public static void print_add_journal_form(HttpServletResponse response)
		throws IOException, ServletException
	{
		PrintWriter writer = response.getWriter();
		writer.println("<center><b>Add Journal:</b>" + 
				"<form name=Form1 action=http://www.csf108library.appspot.com/dialog>" +
				"<table>" +
				"<tr>" +
					"<td><B>Title:</B></td>" +
					"<td><input type=textbox name=title_journal value=\"\" size=25></td><br>" +
				"</tr>" +
				"</table>" +
				"<input type=submit>" +
			"</form></center>");
	}
	public static void print_add_int_user_form(HttpServletResponse response)
		throws IOException, ServletException
	{
		PrintWriter writer = response.getWriter();
		writer.println("<center><b>Add User With ID:</b>" + 
				"<form name=Form1 action=http://www.csf108library.appspot.com/dialog>" +
				"<table>" +
				"<tr>" +
					"<td><B>Name:</B></td>" +
					"<td><input type=textbox name=name value=\"\" size=25></td><br>" +
				"</tr>" +
				"<tr>" +
					"<td><B>Address:</B></td>" +
					"<td><input type=textbox name=address value=\"\" size=25></td>" +
				"</tr>" +
				"<tr>" +
					"<td><B>Phone:</B></td>" +
					"<td><input type=textbox name=phone value=\"\" size=25></td>" +
				"</tr>" +
				"<tr>" +
					"<td>- printAllDocs <br></td>" +
					"<td>- printAllUsers <br></td>" +
				"</tr>" +
				"<tr>" +
					"<td align=center colspan=2>- exit & save<br></td>" +
					"<td><B>ID:</B></td>" +
					"<td><input type=textbox name=id value=\"\" size=25></td>" +
				"</tr>" +
				"</table>" +
				"<input type=submit>" +
			"</form></center>");
	}
	public static void print_user_form(HttpServletResponse response)
		throws IOException, ServletException
	{
		PrintWriter writer = response.getWriter();
		writer.println("<center><b>Print User:</b>" + 
				"<form name=Form1 action=http://www.csf108library.appspot.com/dialog>" +
				"<table>" +
				"<tr>" +
					"<td><B>User ID:</B></td>" +
					"<td><input type=textbox name=user_id value=\"\" size=25></td><br>" +
				"</tr>" +
				"</table>" +
				"<input type=submit>" +
			"</form></center>");
	}
	public static String getCommand(HttpServletRequest request)
	{
		//Reading of inputed instruction
		String command = "";
		@SuppressWarnings("rawtypes")
		Enumeration e = request.getParameterNames();
		if (e.hasMoreElements()) 
		{
			command = (String)e.nextElement();
			command = request.getParameter(command);
		}
		return command;
	}
	public static String[] getArguments(String command)
	{
		String arguments[] = new String[0];
		String temp = command.trim();
		if (temp.indexOf(" ") != -1)
		{
			temp = temp.substring(temp.indexOf(" "));
			arguments = temp.trim().split("/");
			for (int i = 0; i < arguments.length; i++)
				arguments[i] = arguments[i].trim();
		}
		return arguments;
	}	
	public static void addUser(String command, HttpServletResponse response)
		throws IOException, ServletException
	{
		PrintWriter writer = response.getWriter();
		String arguments[] = getArguments(command);
		if (arguments.length < 3)
		{
			writer.println("<br><center>Not all data inputted due to command \"addUser\"</center>");
		}
		else
		{
			User user = new User(arguments[0], arguments[1], arguments[2]);
			library.addUser(user);
			writer.println("<br><center>The user<b> \"" + user.getName() + "\"</b> has been successfully added. ID = <b>" + (user.getCode() + 1) + "</b></center>");
		}
	}
	public static void addBook(String command, HttpServletResponse response)
		throws IOException, ServletException
	{
		PrintWriter writer = response.getWriter();
		String arguments[] = getArguments(command);
		if (arguments.length < 3)
		{
			writer.println("<br><center>Not all data inputted due to command \"addBook\"</center>");
		}
		else
		{
			Document doc  = new Book(arguments[0], arguments[1], arguments[2]);
			library.addDocument(doc);
			writer.println("<br><center>The book<b> \"" + doc.getTitle() + "\"</b> has been successfully added. ID = <b>" + (doc.getCode() + 1) + "</b></center>");
		}
	}
	public static void addReport (String command, HttpServletResponse response)
		throws IOException, ServletException
	{
		PrintWriter writer = response.getWriter();
		String arguments[] = getArguments(command);
		if (arguments.length < 3) 
		{
			writer.println("<br><center>Not all data inputted due to command \"addReport\"</center>");
		}
		else
		{
			TechnicalReport report = new TechnicalReport(arguments[0], arguments[1], arguments[2]);
			library.addDocument(report);
			writer.println("<br><center>The report<b> \"" + report.getTitle() + "\"</b> has been successfully added. ID = <b>" + (report.getCode() + 1) + "</b></center>");
		}
	}
	public static void addJournal(String command, HttpServletResponse response)
		throws IOException, ServletException
	{
		PrintWriter writer = response.getWriter();
		String arguments[] = getArguments(command);
		if (arguments.length < 1) 
		{
			writer.println("<br><center>Not all data inputted due to command \"addJournal\"</center>");
		}
		else
		{
			Journal journal = new Journal(arguments[0]);
			library.addDocument(journal);
			writer.println("<br><center>The journal<b> \"" + journal.getTitle() + "\"</b> has been successfully added. ID = <b>" + (journal.getCode() + 1) + "</b></center>");
		}
	}
	public static void printUser(String command, HttpServletResponse response)
		throws IOException, ServletException
	{
		PrintWriter writer = response.getWriter();
		String arguments[] = getArguments (command);
		try
		{
			if (arguments.length < 1)
			{
				writer.println("<br><center>Not all data inputted due to command \"printUser\"</center>");
			}
			else
			{
				int exception = Integer.parseInt(arguments[0]);
				User user = library.getUser(exception - 1);
				if (user != null)
				{
					user.printInfo(response);
				}
				else writer.println("<br><center>There is no user with following ID - <b>" + arguments[0] + "</b>");
			}
		}
		catch(NumberFormatException e)
		{
			writer.println("<br><center><b>Invalid ID</b></center>");
		}
	}
	public static void printDoc(String command, HttpServletResponse response)
		throws IOException, ServletException
	{
		PrintWriter writer = response.getWriter();
		String arguments [] = getArguments(command);
		try
		{
			if (arguments.length < 1)
			{
				writer.println("<center>Not all data inputted due to command \"printDoc\"</center>");
			}
			else
			{
				int exception = Integer.parseInt(arguments[0]);
				Document doc = library.getDocument(exception - 1);
				if (doc != null)
				{
					doc.printInfo(response);
				}
				else writer.println("<center>There is no document with following ID - <b>" + arguments[0] + "</b>");
			}
		}
		catch(NumberFormatException e)
		{
			writer.println("<br><center><b>Invalid ID</b></center>");
		}
	}	
	public static void printAllDocs(HttpServletResponse response)
		throws IOException, ServletException

	{		
		PrintWriter writer = response.getWriter();
		if (library.documents.size() != 0)
		{
			@SuppressWarnings("rawtypes")
			Iterator i = library.documents.values().iterator();
			//Document doc = library.getDocument(0);
			while(i.hasNext())
			{
				Document d = (Document)i.next();
				d.printInfo(response);
				writer.println("<br />");
			}
		}
		else writer.println("<center>The aren't documents. </center>");	

	}	
	public static void printAllUsers(HttpServletResponse response)
		throws IOException, ServletException
	{
		PrintWriter writer = response.getWriter();
		if (library.users.size() != 0)
		{
			@SuppressWarnings("rawtypes")
			Iterator i = library.users.values().iterator();
			while(i.hasNext())
			{
				User user = (User)i.next();
				user.printInfo(response);
				writer.println("<br />");
			}
		}
		else writer.println("<br><center>There aren't users. </center>");	
}
	public static void searchUser(String command, HttpServletResponse response)
		throws IOException, ServletException
	{
		PrintWriter writer = response.getWriter();
		String arguments [] = getArguments(command);
		if (arguments.length < 1)
		{
			writer.println("<br><center>Not all data inputted due to command \"searchUser\"</center>");
		}
		else
		{
			@SuppressWarnings("rawtypes")
			List users = library.searchUser(arguments[0]);
			@SuppressWarnings("rawtypes")
			Iterator i = users.iterator();
			if (i.hasNext()) writer.println("<br><center><b>Users are found:</b><br>");
			else writer.println("<br><b>Users aren't found.</b><br>");
			while (i.hasNext())
			{
				User user = (User)i.next();
				writer.println("User ID: " + (user.getCode() + 1) + "<br>");
				writer.println("User name: " + user.getName() + "<br><br>");
			}
			writer.println("</center>");
		}
	}
	public static void searchDoc(String command, HttpServletResponse response)
		throws IOException, ServletException
	{
		PrintWriter writer = response.getWriter();
		String arguments[] = getArguments(command);
		if (arguments.length < 1)
		{
			writer.println("<br><center>Not all data inputted due to command \"searchDoc\"</center>");
		}
		else
		{
			@SuppressWarnings("rawtypes")
			List docs = library.searchDocumentByTitle(arguments[0]);
			@SuppressWarnings("rawtypes")
			Iterator i = docs.iterator();
			if (i.hasNext()) writer.println("<br><center><b>Documents are found:</b><br>");
			else writer.println("<br><b>Documents aren't found.</b><br>");
			while (i.hasNext())
			{
				Document doc = (Document)i.next();
				writer.println("Document ID: " + (doc.getCode() + 1) + "<br>");
				writer.println("Document title: " + doc.getTitle() + "<br><br>");
			}
			writer.println("</center>");
		}
	}		
	public static void removeUser(String command, HttpServletResponse response) 
		throws IOException, ServletException
	{
		PrintWriter writer = response.getWriter();
		String arguments[] = getArguments(command);
		if (arguments.length < 1)
		{
			writer.println("<br><center>Not all data inputted due to command \"removeUser\"</center>");
		}
		else
		{
			User user = library.getUser(Integer.parseInt(arguments[0]) - 1);
			if (user == null)
			{
				writer.println("<br><center>No users with following ID - <b>" + (Integer.parseInt(arguments[0])) + "</b></center>");
			}
			else if(library.removeUser(Integer.parseInt(arguments[0]) - 1))
			{
				writer.println("<br><center>The user <b>\"" + user.getName() + "\"</b> " + "with ID - <b>" + (user.getCode() + 1) + "</b> - has been successfully removed.</center>");
			}
		}
	}
	public static void removeDocument (String command, HttpServletResponse response)
		throws IOException, ServletException
	{
		PrintWriter writer = response.getWriter();
		String arguments[] = getArguments(command);
		if (arguments.length < 1) 
		{
			writer.println("<br><center>Not all data inputted due to command \"removeDocument\"</center>");
		}
		else
		{
			Document doc = library.getDocument(Integer.parseInt(arguments[0]) - 1);
			if (doc == null)
			{
				writer.println("<br><center>No documents with following ID - <b>" + (Integer.parseInt(arguments[0])) + "</b><.center>");
			}
			else if (library.removeDocument(Integer.parseInt(arguments[0]) - 1))
			{
				writer.println("<br><center>Document <b>\"" + doc.getTitle() + "\"</b>" + "with ID - <b>" + (doc.getCode() + 1) + "</b> - has been successfully removed.</center>");
			}
		}
	}
	public static void addIntUser(String command, HttpServletResponse response)
		throws IOException, ServletException
	{
		PrintWriter writer = response.getWriter();
		String arguments[] = getArguments (command);
		try
		{
			if (arguments.length < 4)
			{
				writer.println("<br><center>Not all data inputted due to command \"addIntUser\"</center>");
			}
			else
			{
				@SuppressWarnings("unused")
				int exception = Integer.parseInt(arguments[3]);
				User user  = new User(arguments[0], arguments[1], arguments[2], arguments[3]);
				library.addUser(user);
				writer.println("<br><center>The user<b> \"" + user.getName() + "\"</b> has been successfully added. ID = <b>" + (user.getCode() + 1) + "</b></center>");
			}
		}
		catch(NumberFormatException e)
		{
			writer.println("<br><center><b>Invalid ID</b></center>");
		}
	}
	public static void isHolding(String command, HttpServletResponse response)
		throws IOException, ServletException
	{
		PrintWriter writer = response.getWriter();
		String arguments[] = getArguments(command);
		if (arguments.length < 2)
		{
			writer.println("<br><center>Not all data inputted due to command \"isHolding\"</center>");
		}
		else
		{
			User user = library.getUser(Integer.parseInt(arguments[0]) - 1);
			Document doc = library.getDocument(Integer.parseInt(arguments[1]) - 1);
			if (doc == null)
			{
				writer.println("<br><center>There is no document with following ID - <b>" + arguments[0] + "</b>");				
			}
			else if (user == null)
			{
				writer.println("<br><center>There is no user with following ID - <b>" + arguments[1] + "</b>");
			}
			else if (library.isHolding(user, doc))
			{
				writer.println("<center>The user <b>\"" + user.getName() +	"\"</b> holds the document <b>\"" + doc.getTitle() + "\"</b></center>");
			}
			else writer.println("<center>The user <b>\"" + user.getName() +	"\"</b> is not holding the document <b>\"" + doc.getTitle() + "\"</b></center>");
		}
	}	
	public static void borrowDoc(String command, HttpServletResponse response)
	throws IOException, ServletException
	{
		PrintWriter writer = response.getWriter();
		String arguments[] = getArguments(command);
	
		if (arguments.length < 2) 
		{
			writer.println("<br><center>Not all data inputted due to command \"borrowDoc\"</center>");
		}
		else
		{
			User user = library.getUser(Integer.parseInt(arguments[0]) - 1);
			Document doc  = library.getDocument(Integer.parseInt(arguments[1]) - 1);
			if (doc == null)
			{
				writer.println("<br><center>There is no document with following ID - <b>" + arguments[0] + "</b>");				
			}
			else if (user == null)
			{
				writer.println("<br><center>There is no user with following ID - <b>" + arguments[1] + "</b>");
			}
			else
			{
				if (library.borrowDocument(user, doc))
				{
					writer.println("<br><center>New loan has been successully made: <br> The user <b>\"" + user.getName() + "\"</b> ordered document <b>\"" + doc.getTitle() + "\"</b></center><br>");
				}
				else writer.println("<br><center>The document <b>\"" + doc.getTitle() + "\"</b> is not available now.</center>");
			}
		}
	}
	@SuppressWarnings("unused")
	public static void returnDoc(String command, HttpServletResponse response)
		throws IOException, ServletException
	{
		PrintWriter writer = response.getWriter();
		String arguments[] = getArguments(command);
		if (arguments.length < 2)
		{
			writer.println("<center>Not all data inputted due to command \"returnDoc\".</center>");
		}
		else
		{
			Document doc = library.getDocument(Integer.parseInt(arguments[0]) - 1);
			User user = doc.getBorrower();
			if (doc == null)
			{
				writer.println("<center>There is no document with following ID - <b>" + arguments[0] + "</b>");
			}
			else
			if (user == null)
			{
				writer.println("<center>There is no user with following ID - <b>" + arguments[1] + "</b>");
			}
			else if (library.returnDocument(doc))
			{
				writer.println("Loan closed: " + user.getName() +
						" - " + doc.getTitle());
			}
		}
	}		
	public static void processCommand(String command, HttpServletResponse response)
		throws IOException, ServletException
	{
		PrintWriter writer = response.getWriter();
		if ((command.startsWith("exit") || command.startsWith("save")) && command.length() == 4)
		{
			writer.println("<br><center>Information is saved. You may close the window.</center>");
			writer.println("<form><center><input type=button id=btnClose value=\"Close Window\" onclick=\"window.open('', '_self', '');window.parent.close()\"></center></form>");
		}
		else if (command.startsWith("addUser"))
		{
			addUser(command, response);
		}
		else if (command.startsWith("addBook"))
		{
			addBook(command, response);
		}
		else if (command.startsWith("addReport"))
		{
			addReport(command, response);
		}
		else if (command.startsWith("addJournal"))
		{
			addJournal(command, response);
		}
		else if (command.startsWith("printUser"))
		{
			printUser(command, response);
		}
		else if (command.startsWith("printDoc"))
		{
			printDoc(command, response);
		}
		else if (command.startsWith("searchUser"))
		{
			searchUser(command, response);
		}
		else if (command.startsWith("searchDoc"))
		{
			searchDoc(command, response);
		}
		else if (command.startsWith("removeUser"))
		{
			removeUser(command, response);
		}
		else if (command.startsWith("removeDoc"))
		{
			removeDocument(command, response);
		}
		else if (command.startsWith("addIntUser"))
		{
			addIntUser(command, response);
		}
		else if (command.startsWith("borrowDoc"))
		{
			borrowDoc(command, response);
		}
		else if (command.startsWith("isHolding"))
		{
			isHolding(command, response);
		}
		
		
		
/*else if (command.startsWith("returnDoc"))
{
	returnDoc(command, response);
}*/
		// if (command.startsWith("returnDoc"))
		// returnDoc(command);

		// if (command.startsWith("printLoans"))
		// library.printAllLoans();
	}

}

/*
class Main
{
	public static void returnDoc(String command)
	{
		String arguments[] = getArguments(command);
		if (arguments.length < 1) return;
		Document doc = library.getDocument(Integer.parseInt(arguments[0]));
		if (doc == null) return;
		User user = doc.getBorrower();

		if (user == null)
			return;

		if (library.returnDocument(doc))
			writer.println("Loan closed: " + user.getName() +
			" - " + doc.getTitle());
	}
*/